package com.example.light.system.common;


import com.example.light.system.common.ErrorCodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author lfy
 * @date 2023-05-25
 * 封装统一的返回对象
 **/
@Data
public class Result<T> implements Serializable {

    /**
     * 操作成功
     */
    public static final int SUCCESS = 200;

    private Integer status;

    private String message;

    private Boolean success;

    private T data;

    public Result() {

    }

    public Result(T data) {
        this.status = SUCCESS;
        this.message = "success";
        this.success = true;
        this.data = data;
    }

    /**
     * description: Result 包含所有参数的构造方法<br>
     *
     * @param status 状态
     * @param message    消息
     * @param data   数据
     */
    public Result(Integer status, String message, T data,Boolean success) {
        this.status = status;
        this.message = message;
        this.data = data;
        this.success = success;
    }


    public static <T> Result<T> success() {
        return new Result(null);
    }

    public static <T> Result<T> success(T data) {
        return new Result(data);
    }

    public static <T> Result<T> success(String message,T data) {
        return new Result(200,message,data,true);
    }


    /**
     * description: error 异常时返回方法<br>
     *
     * @param errorCodeEnum 异常枚举对象
     * @return com.cg.teaching.utils.Result
     */
    public static <T> Result<T> fail(ErrorCodeEnum errorCodeEnum) {
        return new Result(errorCodeEnum.getValue(), errorCodeEnum.getMessage(), null,false);
    }

    public static <T> Result<T> fail(String message) {
        return new Result(ErrorCodeEnum.OPERATION_FAILED.getValue(), message, null,false);
    }

    public static <T> Result<T> build(Integer status, String message,boolean success) {
        return new Result(status, message, null,success);
    }

    public static <T> Result<T> build(Integer status, String message, T data,boolean success) {
        return  new Result(status, message, data,success);
    }


}
